package com.lzm.ydpt.module.mall.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonutils.TimeUtil;
import com.byt.framlib.commonutils.image.ImageLoaderUtils;
import com.byt.framlib.commonutils.rx.RxBus;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.NoScrollListview;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.RoundedConnerImageView;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.byt.framlib.commonwidget.selectordialog.dialog.DialogOnClickListener;
import com.byt.framlib.commonwidget.selectordialog.dialog.NormalAlertDialog;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.MallOrderBean;
import com.lzm.ydpt.entity.mall.MallOrderListBus;
import com.lzm.ydpt.entity.mall.ShopGoods;
import com.lzm.ydpt.module.mall.dialog.CancalOrderDialog;
import com.lzm.ydpt.module.mall.utils.ProductUtil;
import com.lzm.ydpt.mvp.contract.MallOrderDetailsContract;
import com.lzm.ydpt.mvp.presenterimpl.MallOrderDetailsPresenterImpl;
import com.lzm.ydpt.utils.RxTimerUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单详情页
 */
public class MallOrderDetailsActivity extends BaseActivity<MallOrderDetailsPresenterImpl> implements MallOrderDetailsContract.View {

    @BindView(R.id.ntb_order_details)
    NormalTitleBar ntb_order_details;
    @BindView(R.id.ltip_order_details)
    LoadingTip ltip_order_details;
    @BindView(R.id.srf_order_details)
    RefreshLayout srf_order_details;

    @BindView(R.id.img_health_order_state)
    ImageView img_health_order_state;
    @BindView(R.id.tv_health_order_state)
    TextView tv_health_order_state;

    @BindView(R.id.ll_payment_info)
    LinearLayout ll_payment_info;
    @BindView(R.id.tv_payment_required)
    TextView tv_payment_required;

    @BindView(R.id.tv_remain_time)
    TextView tv_remain_time;

    @BindView(R.id.tv_go_payment)
    TextView tv_go_payment;

    @BindView(R.id.tv_health_order_time)
    TextView tv_health_order_time;

    @BindView(R.id.rl_recevi_address)
    RelativeLayout rl_recevi_address;
    @BindView(R.id.img_address_defalt)
    ImageView img_address_defalt;
    @BindView(R.id.tv_address_user_name)
    TextView tv_address_user_name;
    @BindView(R.id.tv_address_user_phone)
    TextView tv_address_user_phone;
    @BindView(R.id.tv_receiving_address)
    TextView tv_receiving_address;

    @BindView(R.id.rl_order_logistics_info)
    RelativeLayout rl_order_logistics_info;
    @BindView(R.id.tv_order_logistics_desc)
    TextView tv_order_logistics_desc;
    @BindView(R.id.tv_order_logistics_time)
    TextView tv_order_logistics_time;

    @BindView(R.id.tv_store_name)
    TextView tv_store_name;
    @BindView(R.id.lv_health_product)
    NoScrollListview lv_health_product;

    @BindView(R.id.ll_health_order_no)
    LinearLayout ll_health_order_no;
    @BindView(R.id.tv_health_order_no)
    TextView tv_health_order_no;

    @BindView(R.id.ll_over_order_time)
    LinearLayout ll_over_order_time;
    @BindView(R.id.tv_over_order_time)
    TextView tv_over_order_time;

    @BindView(R.id.ll_pay_order_time)
    LinearLayout ll_pay_order_time;
    @BindView(R.id.tv_pay_order_time)
    TextView tv_pay_order_time;

    @BindView(R.id.ll_deliver_order_time)
    LinearLayout ll_deliver_order_time;
    @BindView(R.id.tv_deliver_order_time)
    TextView tv_deliver_order_time;

    @BindView(R.id.ll_deal_order_time)
    LinearLayout ll_deal_order_time;
    @BindView(R.id.tv_deal_order_time)
    TextView tv_deal_order_time;

    @BindView(R.id.ll_order_pay_mode)
    LinearLayout ll_order_pay_mode;
    @BindView(R.id.tv_order_pay_mode)
    TextView tv_order_pay_mode;

    @BindView(R.id.ll_order_delivery_mode)
    LinearLayout ll_order_delivery_mode;
    @BindView(R.id.tv_order_delivery_mode)
    TextView tv_order_delivery_mode;

    @BindView(R.id.tv_product_total)
    TextView tv_product_total;
    @BindView(R.id.rl_freight_total)
    RelativeLayout rl_freight_total;
    @BindView(R.id.tv_freight_total)
    TextView tv_freight_total;

    @BindView(R.id.rl_order_gifs_coin)
    RelativeLayout rl_order_gifs_coin;
    @BindView(R.id.tv_order_gifs_coin)
    TextView tv_order_gifs_coin;

    @BindView(R.id.tv_order_total_title)
    TextView tv_order_total_title;
    @BindView(R.id.tv_order_total)
    TextView tv_order_total;

    @BindView(R.id.rl_show_state_data)
    RelativeLayout rl_show_state_data;
    @BindView(R.id.tv_redund_state)
    TextView tv_redund_state;
    @BindView(R.id.tv_order_use_right)
    TextView tv_order_use_right;
    @BindView(R.id.tv_order_use_left)
    TextView tv_order_use_left;

    private LvCommonAdapter<ShopGoods> mRvGoodsAdapter = null;
    private List<ShopGoods> mGoodsList = new ArrayList<>();

    private long order_id;
    private MallOrderBean mOrderBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_order_details;
    }

    @Override
    public MallOrderDetailsPresenterImpl initPreData() {
        return new MallOrderDetailsPresenterImpl(this);
    }

    @Override
    public void initView() {
        order_id = getIntent().getLongExtra(Constants.INP_ORDER_ID, 0L);
        initTitle();
        initAdapter();
        initRv();
        ltip_order_details.setLoadingTip(LoadingTip.LoadStatus.loading);
        getOrderDetails();
        addSubscription(RxBus.getDefault().toObservableSticky(MallOrderListBus.class).subscribe(
                orderStatueEvent -> {
                    if (orderStatueEvent.getType() == 4) {
                        getOrderDetails();
                    }
                }));
    }

    private void initRv() {
        setRefreshLayout(srf_order_details);
        srf_order_details.setEnableLoadMore(false);
        srf_order_details.setRefreshHeader(new RefreshHeaderView(this).getHeaderStyleStaffF4());
        srf_order_details.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getOrderDetails();
            }
        });
    }

    private void initAdapter() {
        mRvGoodsAdapter = new LvCommonAdapter<ShopGoods>(mBContext, mGoodsList, R.layout.item_order_details_product) {
            @Override
            protected void convert(LvViewHolder viewHolder, ShopGoods orderGoods, int position) {
                RoundedConnerImageView rcim_delivery_src = viewHolder.getView(R.id.img_order_product_pic);
                ImageLoaderUtils.display(rcim_delivery_src, orderGoods.getImage_src());
                viewHolder.setText(R.id.tv_order_product_name, orderGoods.getTitle());
                viewHolder.setText(R.id.tv_order_product_sku, orderGoods.getSpec_title());
                viewHolder.setText(R.id.tv_order_product_sku_num, "x" + orderGoods.getTotal());
                viewHolder.setText(R.id.tv_order_product_price, "¥" + orderGoods.getPrice_rmb());
                switch (mOrderBean.getRefund_state()) { // 退款中
                    case 1:
                    case 2:
                        viewHolder.setVisible(R.id.rl_order_refund, false);
                        break;
                    default:
                        if (isRefund() && mGoodsList.size() > 1) {
                            viewHolder.setVisible(R.id.rl_order_refund, true);
                            if (orderGoods.getRefund_state() == 0) {
                                viewHolder.setVisible(R.id.tv_order_refund, mOrderBean.getRefund_flag() == 1);
                                viewHolder.setVisible(R.id.tv_order_refund_state, false);
                            } else if (orderGoods.getRefund_state() == 1) {
                                viewHolder.setVisible(R.id.tv_order_refund, false);
                                viewHolder.setVisible(R.id.tv_order_refund_state, true);
                                viewHolder.setText(R.id.tv_order_refund_state, "退款中");
                            } else if (orderGoods.getRefund_state() == 2) {
                                viewHolder.setVisible(R.id.tv_order_refund, false);
                                viewHolder.setVisible(R.id.tv_order_refund_state, true);
                                viewHolder.setText(R.id.tv_order_refund_state, "退款完成");
                            } else if (orderGoods.getRefund_state() == 3) {
                                viewHolder.setVisible(R.id.tv_order_refund, mOrderBean.getRefund_flag() == 1);
                                viewHolder.setVisible(R.id.tv_order_refund_state, false);
                                viewHolder.setText(R.id.tv_order_refund_state, "退款关闭");
                            } else if (orderGoods.getRefund_state() == 4) {
                                viewHolder.setVisible(R.id.tv_order_refund, false);
                                viewHolder.setVisible(R.id.tv_order_refund_state, true);
                                viewHolder.setText(R.id.tv_order_refund_state, "退款失败");
                            }
                            viewHolder.setOnClickListener(R.id.tv_order_refund, new OnNoDoubleClickListener() {
                                @Override
                                protected void onNoDoubleClick(View v) {
//                                    Bundle refundBundle = new Bundle();
//                                    refundBundle.putInt(Constants.INP_ORDER_REAL_MODE, 1);
//                                    refundBundle.putLong(Constants.INP_ORDER_ID, mOrderBean.getOrder_id());
//                                    refundBundle.putLong(Constants.INP_ORDER_GOODS_ID, orderGoods.getOrder_goods_id());
//                                    refundBundle.putLong(Constants.INP_SHOP_ID, mOrderBean.getStore_id());
//                                    refundBundle.putString(Constants.INP_SHOP_NAME, mOrderBean.getStore_name());
//                                    openActivity(RefundTypeActivity.class, refundBundle);
                                }
                            });
                            viewHolder.setOnClickListener(R.id.tv_order_refund_state, new OnNoDoubleClickListener() {
                                @Override
                                protected void onNoDoubleClick(View v) {
//                                    Bundle bundle = new Bundle();
//                                    bundle.putLong(Constants.INP_ORDER_REFUND_ID, orderGoods.getOrder_refund_id());
//                                    openActivity(SaleServiceDetailActivity.class, bundle);
                                }
                            });
                        } else {
                            viewHolder.setVisible(R.id.rl_order_refund, false);
                        }
                        break;
                }
            }
        };
        lv_health_product.setAdapter(mRvGoodsAdapter);
    }

    /**
     * 是否可退款
     *
     * @return
     */
    private boolean isRefund() {
        // 可退款而且状态是这几种的情况下
        return mOrderBean.getRefund_flag() == 1 && (mOrderBean.getState() == 4 || mOrderBean.getState() == 5 || mOrderBean.getState() == 6 || mOrderBean.getState() == 7
                || mOrderBean.getState() == 8 || mOrderBean.getState() == 9);
    }

    private void initTitle() {
        ntb_order_details.setTitleText("我的订单");
        ntb_order_details.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onOrderDetails(MallOrderBean orderBean) {

        srf_order_details.finishRefresh();
        if (orderBean != null) {
            ltip_order_details.setLoadingTip(LoadingTip.LoadStatus.finish);
            this.mOrderBean = orderBean;
            initWidget();
        } else {
            ltip_order_details.setLoadingTip(LoadingTip.LoadStatus.empty);
        }
    }

    @Override
    public void onCancalOrder(String msg) {
        stopProgressDialog();
        showShortToast(msg);
        RxBus.getDefault().postSticky(new MallOrderListBus(1));
        mOrderBean.setState(2);
        initWidget();
    }

    @Override
    public void onDeteleOrder(String msg) {
        stopProgressDialog();
        showShortToast(msg);
        RxBus.getDefault().postSticky(new MallOrderListBus(3));
        finish();
    }

    @Override
    public void onReceiptOrder(String msg) {
        stopProgressDialog();
        showShortToast(msg);
        RxBus.getDefault().postSticky(new MallOrderListBus(2));
        mOrderBean.setState(10);
        initWidget();
    }

    private void initWidget() {
        // 1:待付款，2:已取消，3:已关闭，4：待发货（用户付款后） 5：待领取，6：待配送，7：待收货，8：已配送 9:已发货，10：完成
        switch (mOrderBean.getState()) {
            case 1:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                tv_health_order_state.setText("待付款");
                ll_payment_info.setVisibility(View.VISIBLE);
                tv_payment_required.setText("¥" + mOrderBean.getAmount_rmb());
                long time = mOrderBean.getCreated_datetime() * 1000 + Constants.SHOP_PAY_TIME - System.currentTimeMillis();
                tv_remain_time.setText(TimeUtil.getProductOrderTime(time));
                RxTimerUtil.interval(1000, new RxTimerUtil.IRxNext() {
                    @Override
                    public void doNext(long number) {
                        if (!isFinishing()) {
                            long temp = time - number * 1000;
                            tv_remain_time.setText(TimeUtil.getProductOrderTime(temp));
                            if (temp <= 0) {
                                mOrderBean.setState(3);//时间到了，未支付，订单状态改为已关闭
                                initWidget();
                            }
                        }
                    }
                });
                tv_go_payment.setVisibility(View.VISIBLE);
                tv_health_order_time.setVisibility(View.GONE);

                rl_order_logistics_info.setVisibility(View.GONE);

                ll_pay_order_time.setVisibility(View.GONE);
                ll_deliver_order_time.setVisibility(View.GONE);
                ll_deal_order_time.setVisibility(View.GONE);

                setOrderMode(1);

                tv_order_total_title.setText("需付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());
                tv_order_use_left.setVisibility(View.VISIBLE);
                tv_order_use_left.setText("取消订单");
                tv_order_use_right.setVisibility(View.VISIBLE);
                tv_order_use_right.setText("去支付");
                break;
            case 2:
            case 3:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                tv_health_order_state.setText(mOrderBean.getState() == 2 ? "已取消" : "已关闭");
                ll_payment_info.setVisibility(View.GONE);
                tv_go_payment.setVisibility(View.GONE);
                tv_health_order_time.setVisibility(View.GONE);

                rl_order_logistics_info.setVisibility(View.GONE);

                ll_pay_order_time.setVisibility(View.GONE);
                ll_deliver_order_time.setVisibility(View.GONE);
                ll_deal_order_time.setVisibility(View.GONE);

                setOrderMode(1);

                tv_order_total_title.setText("实付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());
                tv_order_use_left.setVisibility(View.GONE);
                tv_order_use_right.setVisibility(View.VISIBLE);
                tv_order_use_right.setText("删除订单");
                break;
            case 4:
            case 6:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                tv_health_order_state.setText(mOrderBean.getState() == 4 ? "待发货" : "待配送");
                ll_payment_info.setVisibility(View.GONE);
                tv_go_payment.setVisibility(View.GONE);
                tv_health_order_time.setVisibility(View.GONE);

                rl_order_logistics_info.setVisibility(View.GONE);
                ll_pay_order_time.setVisibility(View.VISIBLE);
                tv_pay_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getPaid_datetime()));
                ll_deliver_order_time.setVisibility(View.GONE);
                ll_deal_order_time.setVisibility(View.GONE);

                setOrderMode(1);

                tv_order_total_title.setText("实付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());
                initRefundOrderData(null, (mOrderBean.getRefund_flag() == 1 && mOrderBean.getRefund_type() == 0) ? "全部退款" : null);
                break;
            case 5:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                tv_health_order_state.setText("待领取");
                ll_payment_info.setVisibility(View.GONE);
                tv_go_payment.setVisibility(View.GONE);
                tv_health_order_time.setVisibility(View.GONE);

                rl_order_logistics_info.setVisibility(View.GONE);

                ll_pay_order_time.setVisibility(View.VISIBLE);
                tv_pay_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getPaid_datetime()));
                ll_deliver_order_time.setVisibility(View.GONE);
                ll_deal_order_time.setVisibility(View.GONE);

                setOrderMode(1);
                tv_order_total_title.setText("实付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());
                initRefundOrderData("导航到店", (mOrderBean.getRefund_flag() == 1 && mOrderBean.getRefund_type() == 0) ? "全部退款" : null);
                break;
            case 7:
            case 8:
            case 9:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                if (mOrderBean.getState() == 7 || mOrderBean.getState() == 9) {
                    tv_health_order_state.setText("待收货");
                } else if (mOrderBean.getState() == 8) {
                    tv_health_order_state.setText("已配送");
                }
                ll_payment_info.setVisibility(View.GONE);
                tv_go_payment.setVisibility(View.GONE);
                if (mOrderBean.getState() == 7 || mOrderBean.getState() == 9) { // 需要确认收货才可以
                    if (mOrderBean.getReceipt_flag() == 1) {
                        tv_health_order_time.setVisibility(View.VISIBLE);
                        long timeDeliver = mOrderBean.getReceipt_datetime() * 1000 + 3 * 24 * 60 * 60 * 1000 - System.currentTimeMillis();
                        tv_health_order_time.setText("还剩" + TimeUtil.getProductOrderTime(timeDeliver) + "自动确认收货");
                        RxTimerUtil.interval(1000, new RxTimerUtil.IRxNext() {
                            @Override
                            public void doNext(long number) {
                                if (!isFinishing()) {
                                    long temp = timeDeliver - number * 1000;
                                    tv_health_order_time.setText("还剩" + TimeUtil.getProductOrderTime(temp) + "自动确认收货");
                                    if (temp <= 0) {
                                        mOrderBean.setState(10);//时间到了，未确认收货，订单状态改为已完成
                                        initWidget();
                                    }
                                }
                            }
                        });
                    } else {
                        tv_health_order_time.setVisibility(View.GONE);
                    }
                } else {
                    tv_health_order_time.setVisibility(View.VISIBLE);
                    long timeDeliver = mOrderBean.getDeliver_goods_datetime() * 1000 + 3 * 24 * 60 * 60 * 1000 - System.currentTimeMillis();
                    tv_health_order_time.setText("还剩" + TimeUtil.getProductOrderTime(timeDeliver) + "自动确认收货");
                    RxTimerUtil.interval(1000, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext(long number) {
                            if (!isFinishing()) {
                                long temp = timeDeliver - number * 1000;
                                tv_health_order_time.setText("还剩" + TimeUtil.getProductOrderTime(temp) + "自动确认收货");
                                if (temp <= 0) {
                                    mOrderBean.setState(10);//时间到了，未确认收货，订单状态改为已完成
                                    initWidget();
                                }
                            }
                        }
                    });
                }
                if (mOrderBean.getDelivery_method() == 0) {
                    rl_order_logistics_info.setVisibility(View.VISIBLE);
                    tv_order_logistics_desc.setText(mOrderBean.getRemark());
                    tv_order_logistics_time.setText(mOrderBean.getDelivery_datetime());
                } else if (mOrderBean.getDelivery_method() == 1) {
                    // 自取
                    // 二维码
                    rl_order_logistics_info.setVisibility(View.GONE);
                } else {
                    rl_order_logistics_info.setVisibility(View.GONE);
                }
                ll_pay_order_time.setVisibility(View.VISIBLE);
                tv_pay_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getPaid_datetime()));
                ll_deliver_order_time.setVisibility(View.VISIBLE);
                tv_deliver_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getDeliver_goods_datetime()));
                ll_deal_order_time.setVisibility(View.GONE);

                setOrderMode(1);

                tv_order_total_title.setText("实付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());

                //  1:退款中，2:退款完成，3：退款关闭，4：退款失败
                if (mOrderBean.getRefund_state() == 1) {
                    rl_show_state_data.setVisibility(View.VISIBLE);
                    tv_redund_state.setVisibility(View.VISIBLE);
                    tv_redund_state.setText("退款中");
                    tv_order_use_left.setVisibility(View.GONE);
                    tv_order_use_right.setVisibility(View.GONE);
                } else if (mOrderBean.getRefund_state() == 2) {
                    showBacData("退款成功");
                } else if (mOrderBean.getRefund_state() == 3) {
                    rl_show_state_data.setVisibility(View.VISIBLE);
                    tv_redund_state.setVisibility(View.GONE);
                    tv_redund_state.setText("退款关闭");
                    if (mOrderBean.getRefund_flag() == 1 && mOrderBean.getRefund_type() == 0) {
                        tv_order_use_left.setText("全部退款");
                        tv_order_use_left.setVisibility(View.VISIBLE);
                    } else {
                        tv_order_use_left.setVisibility(View.GONE);
                    }
                    tv_order_use_right.setVisibility(View.VISIBLE);
                    tv_order_use_right.setText("确认收货");
                } else if (mOrderBean.getRefund_state() == 4) {
                    rl_show_state_data.setVisibility(View.VISIBLE);
                    tv_redund_state.setVisibility(View.VISIBLE);
                    tv_redund_state.setText("退款失败");
                    tv_order_use_left.setVisibility(View.GONE);
                    tv_order_use_right.setVisibility(View.VISIBLE);
                    tv_order_use_right.setText("确认收货");
                } else { // 正常的
                    tv_redund_state.setVisibility(View.GONE);
                    if (mOrderBean.getRefund_flag() == 1 && mOrderBean.getRefund_type() == 0) {
                        tv_order_use_left.setText("全部退款");
                        tv_order_use_left.setVisibility(View.VISIBLE);
                    } else {
                        tv_order_use_left.setVisibility(View.GONE);
                    }
                    tv_order_use_right.setVisibility(View.VISIBLE);
                    tv_order_use_right.setText("确认收货");
                }
                break;
            case 10:
                img_health_order_state.setImageResource(R.drawable.ic_circle_loading);
                tv_health_order_state.setText("已完成");
                ll_payment_info.setVisibility(View.GONE);
                tv_go_payment.setVisibility(View.GONE);
                tv_health_order_time.setVisibility(View.GONE);

                if (mOrderBean.getDelivery_method() == 0) {
                    rl_order_logistics_info.setVisibility(View.VISIBLE);
                    tv_order_logistics_desc.setText(mOrderBean.getRemark());
                    tv_order_logistics_time.setText(mOrderBean.getDelivery_datetime());
                } else if (mOrderBean.getDelivery_method() == 1) {
                    // 自取
                    // 二维码
                    rl_order_logistics_info.setVisibility(View.GONE);
                } else {
                    rl_order_logistics_info.setVisibility(View.GONE);
                }
                ll_pay_order_time.setVisibility(View.VISIBLE);
                tv_pay_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getPaid_datetime()));
                ll_deliver_order_time.setVisibility(View.VISIBLE);
                tv_deliver_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getDeliver_goods_datetime()));
                ll_deal_order_time.setVisibility(View.VISIBLE);
                tv_deal_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getCompleted_datetime()));

                setOrderMode(0);
                tv_order_total_title.setText("实付款：");
                tv_order_total.setText("¥" + mOrderBean.getAmount_rmb());
                tv_order_use_left.setVisibility(View.GONE);
                tv_order_use_right.setVisibility(View.VISIBLE);
                tv_order_use_right.setText("删除订单");
                break;
        }
        img_address_defalt.setSelected(true);
        tv_address_user_name.setText(mOrderBean.getReal_name());
        tv_address_user_phone.setText(mOrderBean.getMobile());
        tv_receiving_address.setText(mOrderBean.getAddress());

        tv_health_order_no.setText(mOrderBean.getOrder_no());
        tv_over_order_time.setText(TimeUtil.formatData(TimeUtil.dateFormat, mOrderBean.getCreated_datetime()));

        tv_store_name.setText(mOrderBean.getStore_name());
        mGoodsList.clear();
        mGoodsList.addAll(mOrderBean.getGoods());
        mRvGoodsAdapter.notifyDataSetChanged();

        tv_product_total.setText("¥"+mOrderBean.getGoods_amount_rmb());
        tv_freight_total.setText(String.valueOf(mOrderBean.getDispatch_price()));
        tv_order_gifs_coin.setText(String.valueOf(mOrderBean.getGold()));
    }

    private void showBacData(String viewdata) {
        rl_show_state_data.setVisibility(View.VISIBLE);
        tv_redund_state.setVisibility(View.VISIBLE);
        tv_redund_state.setText(viewdata);
        tv_order_use_left.setVisibility(View.GONE);
        tv_order_use_right.setVisibility(View.GONE);
    }

    private void initRefundOrderData(String dataleft, String dataRight) {
        //  1:退款中，2:退款完成，3：退款关闭，4：退款失败
        if (mOrderBean.getRefund_state() == 1) {
            rl_show_state_data.setVisibility(View.VISIBLE);
            tv_redund_state.setVisibility(View.VISIBLE);
            tv_redund_state.setText("退款中");
            if (TextUtils.isEmpty(dataleft)) {
                tv_order_use_left.setVisibility(View.GONE);
            } else {
                tv_order_use_left.setText(dataleft);
                tv_order_use_left.setVisibility(View.VISIBLE);
            }
            tv_order_use_right.setVisibility(View.GONE);
        } else if (mOrderBean.getRefund_state() == 2) {
            showBacData("退款成功");
        } else if (mOrderBean.getRefund_state() == 3) {
            rl_show_state_data.setVisibility(View.VISIBLE);
            tv_redund_state.setVisibility(View.GONE);
            tv_redund_state.setText("退款关闭");
            if (TextUtils.isEmpty(dataleft)) {
                tv_order_use_left.setVisibility(View.GONE);
            } else {
                tv_order_use_left.setText(dataleft);
                tv_order_use_left.setVisibility(View.VISIBLE);
            }
            tv_order_use_right.setVisibility(View.VISIBLE);
            tv_order_use_right.setText(dataRight);
        } else if (mOrderBean.getRefund_state() == 4) {
            rl_show_state_data.setVisibility(View.VISIBLE);
            tv_redund_state.setVisibility(View.VISIBLE);
            tv_redund_state.setText("退款失败");
            if (TextUtils.isEmpty(dataleft)) {
                tv_order_use_left.setVisibility(View.GONE);
            } else {
                tv_order_use_left.setText(dataleft);
                tv_order_use_left.setVisibility(View.VISIBLE);
            }
            tv_order_use_right.setVisibility(View.GONE);
        } else { // 正常的
            tv_redund_state.setVisibility(View.GONE);
            if (TextUtils.isEmpty(dataleft)) {
                tv_order_use_left.setVisibility(View.GONE);
            } else {
                tv_order_use_left.setText(dataleft);
                tv_order_use_left.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(dataRight)) {
                tv_order_use_right.setVisibility(View.GONE);
            } else {
                tv_order_use_right.setVisibility(View.VISIBLE);
                tv_order_use_right.setText(dataRight);
            }
            if (TextUtils.isEmpty(dataleft) && TextUtils.isEmpty(dataRight)) {
                rl_show_state_data.setVisibility(View.GONE);
            }
        }
    }

    private void todoOrderLeftData() {
        switch (mOrderBean.getState()) {
            case 1:
                //取消订单
                CancalOrderDialog dialog = new CancalOrderDialog.Builder(this)
                        .setOnListener(new CancalOrderDialog.ReasonResultCall() {
                            @Override
                            public void onReasonResult(String reason) {
                                startProgressDialog();
                                FormBodys.Builder formBodys = new FormBodys.Builder();
                                formBodys.add("order_id", mOrderBean.getOrder_id());
                                formBodys.add("cancelation_reason", reason);
                                mPresenter.onCancalOrder(formBodys.build());
                            }
                        }).build();
                dialog.show();
                break;
            case 5:

                break;
            case 7:
            case 8:
            case 9:
                //退款
//                Bundle refundBundle = new Bundle();
//                refundBundle.putInt(Constants.INP_ORDER_REAL_MODE, 0);
//                refundBundle.putLong(Constants.INP_ORDER_ID, mOrderBean.getOrder_id());
//                refundBundle.putLong(Constants.INP_SHOP_ID, mOrderBean.getStore_id());
//                refundBundle.putString(Constants.INP_SHOP_NAME, mOrderBean.getStore_name());
//                openActivity(RefundTypeActivity.class, refundBundle);
                break;
        }
    }

    private void todoOrderRightData() {
        switch (mOrderBean.getState()) {
            case 1:
                //去支付
                Bundle bundle = new Bundle();
                bundle.putParcelable(PayOrderActivity.INP_PAY_ORDER, mOrderBean);
                bundle.putInt(PayOrderActivity.INP_PAY_JUMP_TYPE, 2);
                openActivity(PayOrderActivity.class, bundle);
                break;
            case 2:
            case 3:
            case 10:
                deleteOrder();
                break;
            case 4:
            case 5:
            case 6:
                //退款
//                Bundle refundBundle = new Bundle();
//                refundBundle.putInt(Constants.INP_ORDER_REAL_MODE, 0);
//                refundBundle.putLong(Constants.INP_ORDER_ID, mOrderBean.getOrder_id());
//                refundBundle.putLong(Constants.INP_SHOP_ID, mOrderBean.getStore_id());
//                refundBundle.putString(Constants.INP_SHOP_NAME, mOrderBean.getStore_name());
//                openActivity(RefundTypeActivity.class, refundBundle);
                break;
            case 7:
            case 8:
            case 9:
                //确认收货
                if (ProductUtil.isSubmitProduct(mOrderBean)) {
                    NormalAlertDialog normalAlertDialog = new NormalAlertDialog.Builder(mBContext)
                            .setButtonTextSize(14)
                            .setContentText("是否确认收货")
                            .setContentTextSize(14)
                            .setTitleVisible(false)
                            .setLeftButtonText("取消")
                            .setRightButtonText("确定")
                            .setRightButtonTextColor(R.color.color_main)
                            .setLeftButtonTextColor(R.color.color_333333)
                            .setContentTextColor(R.color.color_333333)
                            .setOnclickListener(new DialogOnClickListener() {
                                @Override
                                public void clickLeftButton(View view) {

                                }

                                @Override
                                public void clickRightButton(View view) {
                                    startProgressDialog();
                                    FormBodys.Builder formBodys = new FormBodys.Builder();
                                    formBodys.add("order_id", mOrderBean.getOrder_id());
                                    mPresenter.onReceiptOrder(formBodys.build());
                                }
                            }).build();
                    normalAlertDialog.show();
                } else {
                    showShortToast("售后处理中,不可确认收货");
                }
                break;
        }
    }

    private void deleteOrder() {
        NormalAlertDialog normalAlertDialog = new NormalAlertDialog.Builder(mBContext)
                .setButtonTextSize(14)
                .setContentText("确定删除此订单？")
                .setContentTextSize(14)
                .setTitleVisible(false)
                .setContentTextColor(R.color.color_333333)
                .setOnclickListener(new DialogOnClickListener() {
                    @Override
                    public void clickLeftButton(View view) {
                    }

                    @Override
                    public void clickRightButton(View view) {
                        //删除订单
                        startProgressDialog();
                        FormBodys.Builder formBodys = new FormBodys.Builder();
                        formBodys.add("id", 0);
                        formBodys.add("order_id", mOrderBean.getOrder_id());
                        mPresenter.onDeteleOrder(formBodys.build());
                    }
                }).build();
        normalAlertDialog.show();
    }

    private void setOrderMode(int type) {
        if (type == 0) {
            ll_order_pay_mode.setVisibility(View.GONE);
            ll_order_delivery_mode.setVisibility(View.GONE);
        } else {
            if (mOrderBean.getPayment_platfrom().equals(Constants.PAY_WX)) {
                ll_order_pay_mode.setVisibility(View.VISIBLE);
                tv_order_pay_mode.setText("微信支付");
            } else if (mOrderBean.getPayment_platfrom().equals(Constants.PAY_ALI)) {
                ll_order_pay_mode.setVisibility(View.VISIBLE);
                tv_order_pay_mode.setText("支付宝支付");
            } else {
                ll_order_pay_mode.setVisibility(View.GONE);
            }
            ll_order_delivery_mode.setVisibility(View.VISIBLE);
            if (mOrderBean.getDelivery_method() == 0) {
                tv_order_delivery_mode.setText("普通快递");
            } else if (mOrderBean.getDelivery_method() == 1) {
                tv_order_delivery_mode.setText("自取");
            } else if (mOrderBean.getDelivery_method() == 2) {
                tv_order_delivery_mode.setText("送货上门");
            } else {
                tv_order_delivery_mode.setText("送货上门");
            }
        }
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltip_order_details);
        ltip_order_details.setOnReloadListener(view -> {
            ltip_order_details.setLoadingTip(LoadingTip.LoadStatus.loading);
            getOrderDetails();
        });
    }

    private void getOrderDetails() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("id", 0);
        formBodys.add("order_id", order_id);
        mPresenter.onOrderDetails(formBodys.build());
    }

    @OnClick({R.id.tv_go_payment, R.id.tv_order_use_left, R.id.tv_order_use_right, R.id.rl_order_logistics_info})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_go_payment:
                Bundle bundle = new Bundle();
                bundle.putParcelable(PayOrderActivity.INP_PAY_ORDER, mOrderBean);
                bundle.putInt(PayOrderActivity.INP_PAY_JUMP_TYPE, 2);
                openActivity(PayOrderActivity.class, bundle);
                break;
            case R.id.tv_order_use_left:
                todoOrderLeftData();
                break;
            case R.id.tv_order_use_right:
                todoOrderRightData();
                break;
            case R.id.rl_order_logistics_info:
                Bundle logisBundle = new Bundle();
                logisBundle.putLong(LogisticsDetailsActivity.INP_ORDER_LOGISTICS_NUM, mOrderBean.getOrder_id());
                openActivity(LogisticsDetailsActivity.class, logisBundle);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        RxTimerUtil.cancel();
        super.onDestroy();
    }
}